<?php 
class plus_home
{ 
	function home_plus_list() 
	{ 
		global $mysql_conn,$v_file;
		$list_page = $_GET['list_page'];
		$list_num = $_GET['list_num'];
		$sousuokey = $_GET['sousuokey'];
		$plus_type = $_GET['plus_type'];
		if (empty($list_page)) $list_page = 1;
		if (empty($list_num)) $list_num = 10;
		if ($list_page < 5) {$list_page0 = 1;} else {$list_page0 = $list_page-4;}
		if ($list_page < 5) {$list_page1 = 1;} else {$list_page1 = $list_page-3;}
		if ($list_page < 5) {$list_page2 = 2;} else {$list_page2 = $list_page-2;}
		if ($list_page < 5) {$list_page3 = 3;} else {$list_page3 = $list_page-1;}
		if ($list_page < 5) {$list_page4 = 4;} else {$list_page4 = $list_page;}
		if ($list_page < 5) {$list_page5 = 5;} else {$list_page5 = $list_page+1;}
		if ($list_page < 5) {$list_page6 = 6;} else {$list_page6 = $list_page+2;}
		if ($list_page < 5) {$list_page7 = 7;} else {$list_page7 = $list_page+3;}
		if ($list_page < 5) {$list_page8 = 8;} else {$list_page8 = $list_page+4;}
		$page_btn .= "<button type='button' class='btn btn-white' onClick='list_page_go(".$list_page0.")'><i class='fa fa-chevron-left'></i></button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page1."' onClick='list_page_go(".$list_page1.")'>".$list_page1."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page2."' onClick='list_page_go(".$list_page2.")'>".$list_page2."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page3."' onClick='list_page_go(".$list_page3.")'>".$list_page3."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page4."' onClick='list_page_go(".$list_page4.")'>".$list_page4."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page5."' onClick='list_page_go(".$list_page5.")'>".$list_page5."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page6."' onClick='list_page_go(".$list_page6.")'>".$list_page6."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page7."' onClick='list_page_go(".$list_page7.")'>".$list_page7."</button>";
		$page_btn .= "<button type='button' class='btn btn-white' onClick='list_page_go(".$list_page8.")'><i class='fa fa-chevron-right'></i></button>";
		$list_limit = ($list_page-1)*$list_num.",".$list_num;
		$sql = "SELECT * from `plus` limit ".$list_limit;
		if (!empty($sousuokey)) {
			$sql = "SELECT * from `plus` WHERE `plus_name` = '".$sousuokey."'";
		} 
		if (!empty($plus_type)) {
			$sql = "SELECT * from `plus` WHERE `plus_type` = '".$plus_type."' limit ".$list_limit;
		}
		$result = mysql_query($sql,$mysql_conn);
		while(@$row=mysql_fetch_array($result)) {
			$plus_id = $row['plus_id'];
			$plus_type = $row['plus_type'];
			$plus_name = $row['plus_name'];
			$plus_access = $row['plus_access'];
			$plus_body = $row['plus_body'];
			$sql_type = "SELECT * from `plus_type` WHERE `type_id` = '".$plus_type."'";
			$result_type = mysql_query($sql_type,$mysql_conn);
			@$row_type=mysql_fetch_array($result_type);
			$plus_type_name = $row_type['type_name'];
			$pluslist .= "<div class='col-sm-4'><div class='contact-box'>";
            $pluslist .= "<a href='#'>";
            $pluslist .= "<div class='col-sm-4'><div class='text-center'>";
            $pluslist .= "<img alt='image' class='img-circle m-t-xs img-responsive' src='public/app/".$plus_access."/logo.png'>";
            $pluslist .= "<div class='m-t-xs font-bold'></br><button type='button' class='btn btn-primary btn-xs' ".'onClick="add_plus('.$plus_id.')"'." >添加应用</button></div>";
            $pluslist .= "</div></div><div class='col-sm-8'>";
            $pluslist .= "<h3><strong>".$plus_name."</strong></h3>";
			$pluslist .= "<p>标识符：".$plus_access."</p>";
            $pluslist .= "<p>分类：".$plus_type_name."</p>";
            $pluslist .= "<address>".$plus_body."</address>";
            $pluslist .= "</div><div class='clearfix'></div></a></div></div>";
		}
		$sql = "SELECT * from `plus_type` ";
		$result = mysql_query($sql,$mysql_conn);
		while(@$row=mysql_fetch_array($result)) {
			$type_id = $row['type_id'];
			$type_name = $row['type_name'];
			$typelist .= "<a href='#' class='btn btn-primary btn-xs' onclick='plus_type(".$type_id.");'>".$type_name."</a>";
		}
		require($v_file);
	}
	function home_plus_add() 
	{ 
		global $mysql_conn,$v_file;
		$user_id = $_COOKIE['user_id'];
		$plus_id = $_POST['plus_id'];
		$botlist .= '<div class="input-group">';
		$botlist .= '<input type="hidden" class="form-control" id="home_plus_add_plusid"  value="'.$plus_id.'" >';
		$botlist .= '<select class="form-control m-b" name="home_plus_add_botlist" id="home_plus_add_botlist">';
        $sql = "SELECT * from `bot` WHERE `bot_user_id` = '".$user_id."'";
		$result = mysql_query($sql,$mysql_conn);
		while(@$row=mysql_fetch_array($result)) {
			$bot_id = $row['bot_id'];
			$bot_name = $row['bot_name'];
			$botlist .= '<option value="'.$bot_id.'" >'.$bot_name.'</option>';
		}
        $botlist .= '</select><span class="input-group-btn">';
		$botlist .= '<button type="button" class="btn btn-primary" onclick="add_plus_do('.$plus_id.')" >确定添加</button> </span></div>';
		echo $botlist;
	}
	function home_plus_add_do() 
	{ 
		global $mysql_conn,$v_file;
		$plus_id = $_POST['plus_id'];
		$bot_id = $_POST['bot_id'];
		$sql = "SELECT * from `plus_bot` WHERE `plus_bot` = '".$bot_id."'";
		$result = mysql_query($sql,$mysql_conn);
		@$row=mysql_fetch_array($result);
		$plus_list = $row['plus_list'];//读取现有的plus
		if (empty($plus_list)) {
			$sql="INSERT INTO plus_bot (plus_bot, plus_list) VALUES ('$bot_id','$plus_id')";
			$result = mysql_query($sql,$mysql_conn);
			@$row=mysql_fetch_array($result);
			echo "应用添加成功";
		} else {
			$plus_list_arr = explode(',',$plus_list); 
			if (in_array($plus_id, $plus_list_arr)) {
				echo "您已经添加过此应用";
			} else {
				array_push($plus_list_arr,$plus_id);
				$new_plus_list = implode(",",$plus_list_arr);
				$sql = "UPDATE `plus_bot` SET `plus_list` = '".$new_plus_list."' WHERE `plus_bot` ='".$bot_id."';";
				$result = mysql_query($sql,$mysql_conn);
				@$row=mysql_fetch_array($result);
				echo "应用添加成功";
			}
		}
	}
} 
/* End of file democontroller.php */ 